﻿using System;
using System.Web.Mvc;
using AmazeUI.bll;
using Jiekou.Com;
using Jiekou.Com.Data;
using Newtonsoft.Json;

namespace Ema.Controllers
{
    public class ApiController : Controller
    {
        private JkBll jkBll = new JkBll();

        // GET: /Service/
        #region Public Methods and Operators

        /// <summary>
        /// The index.
        /// </summary>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult Index()
        {
            return this.View();
        }

        /// <summary>
        /// The post.
        /// </summary>
        /// <param name="json">
        /// The json.
        /// </param>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        [HttpPost]
        public ActionResult Post(string json)
        {
            var subJson = new SubJson(json);

            //var fns = jkBll.Fns();
            var user = jkBll.GetUser(Convert.ToInt32(subJson.Suid));

            var retJson = new RetJson(subJson);

            //验证
            var validate = subJson.Validate(user == null ? "" : user.Key);
            //验证成功
            if (!validate)
            {
                retJson.GetJson(subJson);
            }
            else
            {
                try
                {
                    switch (subJson.Fn)
                    {
                        case "0000":
                            retJson.GetJson(new Jsontest());
                            break;
                        default:
                            retJson.GetJson(new NotFn());
                            break;
                    }
                }
                catch (Exception ex)
                {
                    retJson.GetJson(new NotEx(ex.Message));
                }
            }

            jkBll.Save(subJson, retJson);

            return Content(retJson.JsonString);
        }

        #endregion

        /// <summary>
        ///  测试页面.
        /// </summary>
        /// <returns>
        /// The <see cref="ActionResult"/>.
        /// </returns>
        public ActionResult Test()
        {
            return this.View();
        }

        protected override void Dispose(bool disposing)
        {

            base.Dispose(disposing);
        }

        /// <summary>
        /// 业务数据测试 
        /// </summary>
        /// <param name="fn"></param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Testdata(string fn, string data)
        {
            var subJson = new SubJson
            {
                Fn = fn,
                Data = data ?? string.Empty,
                Suid = "2",
                Check = "QYJsHGDF3HS1UuUeuIqslffIZOoBubfE"
            };

            var serializeObject = JsonConvert.SerializeObject(subJson);

            var md5 = WebCommon.Encryption.Md532(serializeObject);
            subJson.Check = md5;

            return this.Post(JsonConvert.SerializeObject(subJson));
        }
    }

}

